//Demo
layui.use(['layer', 'commons', 'form', 'laytpl', 'laydate', 'element', 'table', 'laypage'], function() {
	var layer = layui.layer,
		$ = layui.jquery,
		common = layui.commons,
		form = layui.form,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table,
		laypage = layui.laypage,
		element = layui.element;
	var ypmc = "";
	var pyjm = "";
	var city = "";
	var page = 1;
	var size = 10;

	//监听提交
	form.on('submit(formDemo)', function(data) {
		console.log(JSON.stringify(data.field));
		ypmc = data.field.ypmc;
		pyjm = data.field.pyjm;
		city = data.field.city;
		console.log(city)
		list(ypmc, city, pyjm, page, size)
		return false;
	});

	//表格
	function list(ypmc, city, pyjm, page, size) {
		common.ajax('/service-nursing/drug/list',
			JSON.stringify({
				"body": {
					"itemClass": city,
					"itemName": ypmc,
					"searchText": pyjm,
					"currPage": page,
					"pageSize": size
				},
				"header": {},
				"token": {}
			}),
			function(data) {
				console.log(data);
				var len = data.data.total;
				console.log("服务端共" + data.data.total + "条数据")
				console.log(data.data.list.length);
				table.render({
					elem: '#ypgl',
					//height: 312,
					page: true //开启分页
						,
					//toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
						//,
					cols: [
						[ //表头
							{
								field: 'id',
								title: 'ID',
								sort: true,
								fixed: 'left'
							}, {
								field: 'code',
								title: '编号',
							}, {
								field: 'searchText',
								title: '简码',
								sort: true
							}, {
								field: 'itemName',
								title: '名称',
							}, {
								field: 'itemSpec',
								title: '规格',
							}, {
								field: 'itemClassName',
								title: '类型',
								sort: true
							}, {
								field: 'untis',
								title: '单位',
								sort: true
							}, {
								field: 'price',
								title: '单价',
							} , {
								// fixed: 'right',
								title: '操作',
								align: 'center',
								toolbar: '#barDemo'
							}
						]
					],
					data: data.data.list
				});

				//分页监听
				laypage.render({
					elem: 'test2',
					count: len, //数据总数，从服务端得到
					curr: 1, //起始页
					// groups: 5,
					limit: 10,
					limits: [10],
					layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
					jump: function(obj, first) {
						//obj包含了当前分页的所有参数，比如：
						console.log("当前页码" + obj.curr); //得到当前页，以便向服务端请求对应页的数据。
						console.log("当前显示" + obj.limit + "条"); //得到每页显示的条数
						//根据当前页请求和渲染模
						currPage = obj.curr
						//首次不执行
						if (!first) {
							//do something
							list(currPage)
						}
					}
				});
			}
		)
	}

	list(ypmc, city, pyjm, page, size)

	//监听行工具事件
	table.on('tool(ypgl)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
		console.log(obj.data);
		var data = obj.data //获得当前行数据
			,
			layEvent = obj.event; //获得 lay-event 对应的值
		if (layEvent === 'detail') {
			layer.msg('查看操作');
		} else if (layEvent === 'del') {
			layer.confirm('真的删除行么', function(index) {
				obj.del(); //删除对应行（tr）的DOM结构
				layer.close(index);
				//向服务端发送删除指令
			});
		} else if (layEvent === 'edit') {
			// layer.msg('编辑操作');
			console.log(obj.data.id);
			window.location.href = "./ypbz.html?id=" + obj.data.id;
		}
	});
});
